package cn.hesion.thread.futureandpromise;
/**
 * ClassName: FutureAndPromise <br/>
 * Description: <br/>
 * date: 2022/4/30 14:10<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.concurrent.*;

/**
 * Future模式和Promise模式的学习
 * https://www.cnkirito.moe/future-and-promise/
 * @author: hesion
 * @create: 2022-04-30 14:10
 **/
public class FutureAndPromise {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        long l = System.currentTimeMillis();
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<Integer> future = executorService.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("执行耗时操作.....");
                timeConsumingOperation();
                return 100;
            }
        });
        System.out.println("计算结果:"+future.get());
        System.out.println("主线程运行耗时:"+(System.currentTimeMillis()-l));
    }

    private static void timeConsumingOperation() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
